package com.hnkywl.sts.dao.base;

import com.hnkywl.sts.entity.base.Dxpz;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-20
 * Time: 下午8:48
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class DxpzDao extends SimpleHibernateTemplate<Dxpz, Long> {
    public Page<Dxpz> findDxpzList(String mc,int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Dxpz> page = new Page<Dxpz>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (mc != null && !mc.isEmpty()) {
            condition.append(" mc like ? ");
            param.add("%" + mc.trim() + "%");
        }
        //==============记录数量===============
        hql = "select count(*) from Dxpz";
        if (condition.length() != 0)
            hql = hql + " where " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Dxpz ";
        if (condition.length() != 0)
            hql = hql + " where " + condition;
        hql = hql + " order by dateCreated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public Dxpz findByBm(String pzbm) {
        List<Dxpz> dxpzs = this.find("from Dxpz where dxbm = ? and sfqy = true",pzbm);
        return dxpzs.size() == 0 ? null : dxpzs.get(0);
    }
}
